from src.vobjects.userplaylist import UserPlaylist
from src.vobjects.video import Video

from src.database import Database

import json

class UserPlaylists:

    def __init__(self):
        self.database = Database()
        self.playlists = []
        self.load_playlists()

    def load_playlists(self):
        for item in self.database.load_user_playlists():
            try:
                name = item[0]
                videos_text = item[1].replace("\"", "`")
                videos_text = videos_text.replace("\'", "\"")
                videos_uncode = json.loads(videos_text)
                videos = []
                for vid in videos_uncode:
                    video = Video().from_history_dict(vid)
                    videos.append(video)
                videos.reverse()
                playlist = UserPlaylist(name, videos)
                self.playlists.append(playlist)
            except: continue
        self.playlists.reverse()

    def videos_to_str(self, videos: list):
        svideos = []
        for video in videos:
            dct = video.to_history_dict()
            svideos.append(dct)
        return str(svideos)
    
    def get_playlist(self, name: str):
        for playlist in self.playlists:
            if playlist.name == name: return playlist
        return None

    def remove_from_list(self, name: str):
        self.playlists.remove(self.get_playlist(name))

    def new(self, name: str, videos: list):
        svideos = self.videos_to_str(videos)
        playlist = UserPlaylist(name, videos)
        self.playlists.insert(0, playlist)
        self.database.insert_user_playlist(name, svideos)

    def insert(self, name: str, video: Video):
        item = self.database.get_user_playlist(name)

        playlist = UserPlaylist.from_str(name, item[0])
        
        if playlist == None: return
        playlist.insert(video)

        self.database.remove_user_playlist(name)
        self.remove_from_list(name)
        self.playlists.insert(0, playlist)
        
        svideos = self.videos_to_str(playlist.videos)
        self.database.insert_user_playlist(name, svideos)

    def remove(self, name: str, video: Video):
        item = self.database.get_user_playlist(name)

        playlist = UserPlaylist.from_str(name, item[0])
        
        if playlist == None: return
        playlist.remove(video)

        self.database.remove_user_playlist(name)
        self.remove_from_list(name)
        self.playlists.insert(0, playlist)

        svideos = self.videos_to_str(playlist.videos)
        self.database.insert_user_playlist(name, svideos)

    def remove_playlist(self, name: str):
        for playlist in self.playlists:
            if playlist.name == name: 
                self.playlists.remove(playlist)
                break 
        self.database.remove_user_playlist(name)

if __name__ == "__main__":
    up = UserPlaylists()
    video = Video(title="TestVideo", videoId="tzCbydYoeKY", author="Linus", authorId="yeyeye", authorUrl="trololo", published="", publishedText="", lengthSeconds="")
    up.new("Tester", [video,])

